package com.gt.controller.admin;

import com.gt.pojo.PageResult;
import com.gt.pojo.Result;
import com.gt.pojo.dto.BookDTO;
import com.gt.pojo.dto.BookPageQueryDTO;
import com.gt.pojo.entity.Book;
import com.gt.pojo.vo.BookVO;
import com.gt.service.BookService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;


@RestController(value = "adminBookController")
@Api(tags = "图书相关接口")
@RequestMapping("/admin/book")
public class BookController {

    @Autowired
    private BookService bookService;

    @ApiOperation("新增图书")
    @PostMapping
    public Result save(@RequestBody BookDTO bookDTO){
        bookService.save(bookDTO);
        return Result.success();
    }
    @ApiOperation("图书分页查询")
    @GetMapping("/page")
    public Result<PageResult> page(BookPageQueryDTO bookPageQueryDTO){
        PageResult pageResult = bookService.pageQuery(bookPageQueryDTO);
        return Result.success(pageResult);
    }
    @ApiOperation("批量删除图书")
    @DeleteMapping("/deleteBatch")
    public Result delete(@RequestParam List<Long> ids){
        bookService.deleteBatch(ids);
        return Result.success();
    }
    @ApiOperation("删除图书")
    @DeleteMapping
    public Result<String> deleteById(Long id){
        bookService.deleteById(id);
        return Result.success();
    }
    @ApiOperation("根据Id查找书籍")
    @GetMapping("/{id}")
    public Result<BookVO> getById(@PathVariable Long id){
        BookVO bookVO = bookService.getById(id);
        return Result.success(bookVO);
    }
    @ApiOperation("修改图书")
    @PutMapping
    public Result update(@RequestBody BookDTO bookDTO){
        bookService.update(bookDTO);
        return Result.success();
    }
    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询图书")
    public Result<List<Book>> list(Long categoryId){
        List<Book> list = bookService.list(categoryId);
        return Result.success(list);
    }
}
